﻿using ATMClientWinForm.ATMService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ATMClientWinForm
{
    public partial class Transfer : Form
    {
        Account acc;
        Account accTo;
        ATMService.ATMServiceSoapClient client;
        public Transfer(Account acc)
        {
            InitializeComponent();
            this.acc = acc;
            client = new ATMService.ATMServiceSoapClient();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            LoadAccount();
            ManageATM ad = new ManageATM(this.acc);
            ad.Show();
            this.Close();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (txtIpTo.Text.Trim() != "")
            {
                if (txtIpTo.Text.Trim() != this.acc.Ip)
                {
                    Account accountToLoad = client.GetAccount(txtIpTo.Text.Trim());
                    if (accountToLoad != null)
                    {
                        this.accTo = accountToLoad;
                        txtName.Text = accTo.Name;
                    }
                    else
                        MessageBox.Show("Account not found");
                }
                else
                    MessageBox.Show("Can not transfer to your account");
            }

        }
        void LoadAccount()
        {
            Account account = client.GetAccount(acc.Ip);
            this.acc = account;
        }

        private void txtIpTo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {

                if (e.Handled = !char.IsNumber(e.KeyChar))
                {
                    MessageBox.Show("Must enter number");
                }

            }
        }

        private void txtMoney_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {

                if (e.Handled = !char.IsNumber(e.KeyChar))
                {
                    MessageBox.Show("Must enter number");
                }

            }
        }

        private void btnTransfer_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() != "")
            {
                if (txtMoney.Text.Trim() != "")
                {
                    LoadAccount();
                    int money = Convert.ToInt32(txtMoney.Text.Trim());
                    Transfers(money);
                }
            }
            else
                MessageBox.Show("Press the Load button to confirm information");
        }

        void Transfers(int money)
        {
            if (money < this.acc.Balance)
            {
                if (this.acc.Balance - money > 100000)
                {
                    if (money > 100000 && money < 1000000)
                    {
                        //this.acc.Balance += Convert.ToInt32(txtMoney.Text);
                        GetTransactionForm();
                       
                        //comboBox1_SelectedIndexChanged(this,new EventArgs());
                        
                        bool ExceptMoney = client.ExceptMoney(money, this.acc.Ip);
                        bool AddMoney = client.AddMoney(money, this.accTo.Ip);
                        LoadAccount();
                        Transaction sending = new Transaction
                        {
                            IpFrom = this.acc.Ip,
                            IpTo = this.accTo.Ip,
                            TypeTransaction = "Transfer",
                            TypeTransfer = comboBox1.Text,
                            Amount = Convert.ToInt32(txtMoney.Text),
                            Balance = acc.Balance,
                            Description = rtbContent.Text,
                            time = DateTime.Now
                        };
                        bool SendingTransfer = client.SendingTransfer(sending);

                        if (ExceptMoney == true || AddMoney == true || SendingTransfer == true)
                        {
                            LoadAccount();
                            MessageBox.Show("Transaction Successed..!\n Balance Current of You : " + acc.Balance);
                            txtMoney.Text = "";
                            txtIpTo.Text = "";
                            txtName.Text = "";
                            comboBox1.SelectedIndex = 0;
                            lblCharges.Text = "0 VNĐ";
                            rtbContent.Text = "";
                            txtIpTo.Focus();
                        }
                        else
                            MessageBox.Show("Transfer False..!");
                    }
                    else
                        MessageBox.Show("Money entered must be greater 100.000 and  less than 1.000.000 ");
                }
                else
                    MessageBox.Show("Total balance must be greater 100.000 ");
            }
            else
                MessageBox.Show("Money enter  not exceeding Total Balance");
        }



        public void GetTransactionForm()
        {

            if (rdbb.Checked)
            {
                client.ExceptMoney(8800, txtIpTo.Text);
            }
            else
            {
                client.ExceptMoney(8800, this.acc.Ip);
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                lblCharges.Text = "0 vnđ";
                rdbb.Enabled = false;
                rbs.Enabled = false;
            }

            if (comboBox1.SelectedIndex == 1)
            {
                lblCharges.Text = "8.800 vnđ";
                rdbb.Enabled = true;
                rbs.Enabled = true;
            }
        }


    }
}
